package com.lungle.warmdiary.app.view.me.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.model.response.TesterIndustry;
import com.lungle.warmdiary.app.view.me.view.BaseViewHolder;

import java.util.List;


public class IndustryAdapter extends BaseAdapter {
    private Context mContext;

    private List<TesterIndustry> mTagList, selectedTagList;
    private onMultiItemSelectListener mOnMultiItemClick;
    private int clickTemp = -1;

    public IndustryAdapter(Context mContext, List<TesterIndustry> allTags, List<TesterIndustry> selected, onMultiItemSelectListener mOnMultiItemClick) {
        super();
        this.mContext = mContext;
        this.mTagList = allTags;
        selectedTagList = selected;
        this.mOnMultiItemClick = mOnMultiItemClick;

        for (TesterIndustry tag : selected) {
        }
    }

    public void setSelection(int choiceCount) {
        this.clickTemp = choiceCount;
    }


    @Override
    public int getCount() {
        return mTagList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.view_tag, parent, false);
        }
        TextView num = BaseViewHolder.get(convertView, R.id.tv_tag);
        CheckBox cb = BaseViewHolder.get(convertView, R.id.cb_tag);

        TesterIndustry tag = mTagList.get(position);


        num.setText(tag.getName());


        cb.setVisibility(View.GONE);

        cb.setChecked(false);
        num.setSelected(false);
        if (clickTemp == position) {
            num.setSelected(true);
        } else {
            num.setSelected(false);
        }

        if (isSelected(tag)) {
            cb.setChecked(true);
            num.setSelected(true);
        }

        convertView.setOnClickListener(new onTextClick(position, cb, num));

        return convertView;
    }

    private boolean isSelected(TesterIndustry tag) {
        for (TesterIndustry t : selectedTagList) {
            if (t.getName().equals(tag.getName())) {
                return true;
            }
        }

        return false;

    }

    public interface onMultiItemSelectListener {
        public void onItemClick(View v, int Position, CheckBox checkBox, TextView textView);
    }

    class onTextClick implements View.OnClickListener {
        int position;
        CheckBox checkbox;
        TextView tvSelected = null;
        TextView textview;

        public onTextClick(int position, CheckBox checkbox) {
            this.position = position;
            this.checkbox = checkbox;
        }

        public onTextClick(int position, CheckBox checkBox, TextView textView) {
            this.position = position;
            this.checkbox = checkBox;
            this.textview = textView;
        }


        @Override
        public void onClick(View v) {
            if (mTagList != null && mOnMultiItemClick != null) {
                mOnMultiItemClick.onItemClick(v, position, checkbox, textview);
            }
        }
    }

}
